#include <defs.h>
#include <string.h>

int Atoi(const char* str) {
  int n = 0, i;
  for (i = 0; str[i] >= '0' && str[i] <= '9'; i++) {
    n = (n*10)+(str[i]-'0');
  }
  return n;
}

int IsAlnum(char c) {
  return (c == ' ' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
      (c >= '0' && c <= '9'));
}

int StringCompare(char *str1, char *str2, int n) 
{
	int i;
	for (i = 0; i < n; i++) {
		if (str1[i] != str2[i])
			return 0;			
		if (str1[i] == '\0') 
		  break;
	}
	return 1;
}

void MemCopy(char *target, char *source, int len) {
	int i;
	for (i = 0; i < len; i++) 
		target[i] = source[i];
}

void StringCopy(char *target, char *source) {
	int i;
	for (i = 0; source[i] != '\0' && i < MAX_NAME_LEN; i++) 
		target[i] = source[i];
	target[i] = '\0';
}

char* StringToken(char *str, char **pos, char delim) {
  if (str == NULL) str = *pos;
  if (!(*str)) return NULL;
  int i = 0;
  for (; str[i]; i++) {
    if (str[i] == delim) {
      str[i++] = '\0';
      break;
    }
  }
  *pos = str+i;
  return str;
}
